package com.amqp.demo;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class ConsumerLisener2 extends HelloWorldConfiguration{
    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerLisener2.class);
    }

    @Bean
    public SimpleMessageListenerContainer listenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory());
        container.setQueueNames("queue-101");
        /**
         * 抛出 AmqpRejectAndDontRequeueException  消息会被拒绝，且 requeue = false（不重新入队列）
         * 抛出 ImmediateAcknowledgeAmqpException  消息正常会被确认。
         * 抛出 其他的异常，消息会被拒绝，且 requeue = true （重新入队列），容易造成死循环。，通过setDefaultRequeueRejected（true）
         */
//        container.setAcknowledgeMode(AcknowledgeMode.AUTO); // 根据异常抛出情况自动确认。
        container.setAcknowledgeMode(AcknowledgeMode.NONE); // 自动确认。
//        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 手动确认。

//        container.setDefaultRequeueRejected(false); // 抛出异常默认拒绝，最好开着不然容易死循环

        // channelListener
        //container.setMessageListener(new MessageListenerAdapter(new ReceiveMsgHandler2()));

        // channelListener
        container.setMessageListener(new MessageListenerAdapter(new ReceiveMsgHandler3()));

        return container;
    }


}
